package cz.cacek.ebook;

import java.util.Vector;

import javax.microedition.lcdui.Choice;
import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Gauge;

/**
 * Form with options
 * @author Josef Cacek [josef.cacek (at) gmail.com]
 * @author $Author: kwart $
 * @version $Revision: 1.2 $
 * @created $Date: 2007/11/25 13:01:43 $
 */
public class SettingForm extends Form implements CommandListener {

	//final static BackLight BL = BackLight.getInstance();

	final ChoiceGroup choices = new ChoiceGroup(ResourceProviderME.get("opt.choices"), Choice.MULTIPLE);
	final Gauge backLight = new Gauge(ResourceProviderME.get("opt.backlig"), true, 100, 0);
	final Gauge backLightAS = new Gauge(ResourceProviderME.get("opt.backligas"), true, 100, 0);
	final ChoiceGroup colors = new ChoiceGroup(ResourceProviderME.get("opt.colors"), Choice.EXCLUSIVE);
	final ChoiceGroup fonts = new ChoiceGroup(ResourceProviderME.get("opt.fonts"), Choice.EXCLUSIVE);

	private Command cmdOK = new Command(ResourceProviderME.get("btn.ok"), Command.OK, 1);
	private Command cmdCancel = new Command(ResourceProviderME.get("btn.cancel"), Command.CANCEL, 1);

	private Vector fontVector = new Vector();

	private EBookCanvas canvas;

	private SettingBean setting;

	public SettingForm(final EBookCanvas aCanvas) {
		super(ResourceProviderME.get("opt.head"));
		canvas = aCanvas;
		setting = aCanvas.getSetting();

		setCommandListener(this);

		choices.append(ResourceProviderME.get("opt.wrapword"), null);
//		choices.append(ResourceProviderME.get("opt.fullscreen"), null);
		choices.append(ResourceProviderME.get("opt.rotate"), null);
		choices.append(ResourceProviderME.get("opt.status"), null);
		choices.append(ResourceProviderME.get("opt.posperc"), null);
		int i=0;
		choices.setSelectedIndex(i++, setting.isWrapSpaces());
//		choices.setSelectedIndex(i++, canvas.isFullscreen());
		choices.setSelectedIndex(i++, setting.isRotated());
		choices.setSelectedIndex(i++, setting.isDisplayStatus());
		choices.setSelectedIndex(i++, setting.isPositionAsPerc());

		BackLight BL = setting.getBackLight();
		backLight.setValue(BL.getLight(false));
		backLightAS.setValue(BL.getLight(true));

		final String tmpWhite = ResourceProviderME.get("cl.white");
		final String tmpBlack = ResourceProviderME.get("cl.black");
		final String tmpBlue = ResourceProviderME.get("cl.blue");
		addColors(tmpWhite, tmpBlack);
		addColors(tmpWhite, tmpBlue);
		addColors(tmpBlack, ResourceProviderME.get("cl.green"));
		addColors(tmpBlue, tmpWhite);
		colors.setSelectedIndex(setting.getColorScheme(), true);

		addFont(Font.FACE_PROPORTIONAL, Font.STYLE_PLAIN, Font.SIZE_SMALL);
		addFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_SMALL);
		addFont(Font.FACE_MONOSPACE, Font.STYLE_PLAIN, Font.SIZE_SMALL);
		addFont(Font.FACE_MONOSPACE, Font.STYLE_BOLD, Font.SIZE_SMALL);
		addFont(Font.FACE_PROPORTIONAL, Font.STYLE_PLAIN,Font.SIZE_MEDIUM);
		addFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_MEDIUM);
		addFont(Font.FACE_MONOSPACE, Font.STYLE_PLAIN, Font.SIZE_MEDIUM);
		addFont(Font.FACE_MONOSPACE, Font.STYLE_BOLD, Font.SIZE_MEDIUM);
		addFont(Font.FACE_PROPORTIONAL, Font.STYLE_PLAIN, Font.SIZE_LARGE);
		addFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_LARGE);
		addFont(Font.FACE_MONOSPACE, Font.STYLE_PLAIN, Font.SIZE_LARGE);
		addFont(Font.FACE_MONOSPACE, Font.STYLE_BOLD, Font.SIZE_LARGE);
		int tmpIdx = 0;
		final Font tmpFont = setting.getFont();
		if (tmpFont.getSize()==Font.SIZE_MEDIUM) {
			tmpIdx = 4;
		} else if (tmpFont.getSize()==Font.SIZE_LARGE) {
			tmpIdx = 8;
		}
		if (tmpFont.getFace()==Font.FACE_MONOSPACE) {
			tmpIdx += 2;
		}
		if (tmpFont.getStyle()==Font.STYLE_BOLD) {
			tmpIdx += 1;
		}
		fonts.setSelectedIndex(tmpIdx, true);

		append(fonts);
		append(choices);
		if (BackLight.isLight()) {
			append(backLight);
			append(backLightAS);
		}
		append(colors);

		addCommand(cmdOK);
		addCommand(cmdCancel);
	}

	/**
	 * Adds one font to list
	 * @param aFace proportional flag
	 * @param aStyle bold flag
	 * @param aSize size of font
	 */
	private void addFont(final int aFace, final int aStyle, final int aSize) {
		fontVector.addElement(Font.getFont(aFace, aStyle, aSize));
		final String tmpFace =
			aFace == Font.FACE_PROPORTIONAL ?
				ResourceProviderME.get("f.proportional"):
					ResourceProviderME.get("f.monospace");
		final String tmpStyle =
			aStyle == Font.STYLE_PLAIN ?
				ResourceProviderME.get("f.normal"):
					ResourceProviderME.get("f.bold");
		String tmpSize;
		if (aSize == Font.SIZE_SMALL) {
			tmpSize = ResourceProviderME.get("f.small");
		} else if (aSize == Font.SIZE_MEDIUM) {
			tmpSize = ResourceProviderME.get("f.medium");
		} else {
			tmpSize = ResourceProviderME.get("f.large");
		}
		fonts.append(tmpSize + "/" + tmpFace + "/" + tmpStyle, null);
	}

	/**
	 * Adds color scheme to the list.
	 * @param aBgName name of background color
	 * @param aFgName name of foreground color
	 */
	private void addColors(String aBgName, String aFgName) {
		colors.append(aBgName + "/" + aFgName, null);
	}


	/**
	 * @param aCmd
	 * @param aDisp
	 */
	public void commandAction(Command aCmd, Displayable aDisp) {
		Utils.debug("Options - Command action " + aCmd);
		if (aCmd == cmdCancel) {
			canvas.setBookScreen();
		} else if (aCmd == cmdOK) {
			int i=0;
			setting.setWrapSpaces(choices.isSelected(i++));
//			canvas.setFullscreen(choices.isSelected(i++));
			setting.setRotated(choices.isSelected(i++));
			setting.setDisplayStatus(choices.isSelected(i++));
			setting.setPositionAsPerc(choices.isSelected(i++));

			BackLight BL = canvas.getSetting().getBackLight();
			BL.setLight(false, backLight.getValue());
			BL.setLight(true, backLightAS.getValue());

			setting.setColorScheme(colors.getSelectedIndex());

			final Font font = (Font) fontVector.elementAt(fonts.getSelectedIndex());
			try {
				setting.setFont(font);
				this.canvas.getView().initSetting(setting);

			} catch (Exception e) {
				Utils.error(e);
			}
			canvas.setBookScreen();
		}
	}
}
